package core

import (
	. "filter/file"
	"fmt"
)

type Routiner interface {
	Process(interface{})
}

func RountineFileGet(folders *[]Folder, max int) Filters {
	// maxchan := max
	chs := make([]chan int, max)
	cnt := 0
	flag := false
	var ftr Filters
	ftr.Init()
	ftr.SetEncoding("CRC32")

	for i, _ := range chs {
		chs[i] = make(chan int)
	}
	fsize := len(*folders)
	fcount := 0
	fld := *folders
	for {
		if fcount >= fsize {
			break
		}
		if !flag {
			fmt.Println("检查文件夹", fld[fcount].Name, cnt)
			go ThreadProcessFolder(&ftr, &fld[fcount], chs[cnt])
			fcount++
			cnt++

			if cnt >= max-1 {
				flag = true
			}
		} else {
			// if cnt >= max-1 {
			// 	cnt = 0
			// } else {
			// 	cnt++
			// }
			for i, ch := range chs {
				<-ch
				fmt.Println("检查文件夹", fld[fcount].Name, cnt)
				go ThreadProcessFolder(&ftr, &fld[fcount], chs[i])
				fcount++

			}

		}
	}

	return ftr
}

func ThreadProcessFolder(ftr *Filters, fld *Folder, ch chan int) {
	// fmt.Println(str)
	if ftr != nil && fld != nil {
		ftr.ProcessFolder(*fld)
	}
	ch <- 1
}

func ChanGet(types string, folder Folder, cf chan *File) {
	// var folder Folder
	// if()

	// cf <- file
}
